<template>
  <div class="for-present">
    <EditTable
      ref="editTable"
      @onChangeSelectAll="onSelectAll"
      @onJoinSale="onJoinSale"
      @onJoinSaleCheck="onJoinSaleCheck"
      :listData="columns1"
      @onDeleteSale="onDeleteSale"
      :list="$store.state.fullGiftSale.GetgiftData3"
    ></EditTable>
  </div>
</template>
<script>
import EditTable from "../edit/PresentTable1";
import {setArrayValue, getValueArray, setEmptyArray, getSelectStatus, deleteValueArray, getValueStatus} from "./ChoiceCommidyMethods.js";
import { columns1 } from "./presentMethods.js";
import { deleteSale } from "@/views/sales/common/TableSaleMethods.js";
export default {
  name: "forpresent",
  components: {
    EditTable
  },
  data() {
    return {
      columns1,
      data1: []
    };
  },
  methods: {
    deleteSaleData() {
      deleteValueArray(this.$store.state.fullGiftSale.GetgiftData3, {index: "checked", childName: "productSkuList", status: true})
      setEmptyArray(this.$store.state.fullGiftSale.GetgiftData3, {index: "checked", childName: "productSkuList"})
      getValueStatus(this.$store.state.fullGiftSale.GetgiftData3, {index: "checked", childName: "productSkuList"})
      // this.$refs.editTable.selectAll = getSelectStatus(this.$store.state.fullGiftSale.GetgiftData3, {index: 'checked', childName: 'productSkuList'})
    },
    onSelectAll(param) {
      //点击选中所有checked按钮的时候出发的事件
      param ? setArrayValue(this.$store.state.fullGiftSale.GetgiftData3, {
        index: "checked",
        val1: true,
        childName: "productSkuList"
      }) : setArrayValue(this.$store.state.fullGiftSale.GetgiftData3, {
        index: "checked",
        val1: false,
        childName: "productSkuList"
      })
    },
    onJoinSaleBody(item, todo) {
      if (item.productSkuList && item.productSkuList.length > 0) {
        item.checked ? setArrayValue(item.productSkuList, {
              index: "checked",
              val1: true,
              childName: "productSkuList"
        })
      : setArrayValue(item.productSkuList, {
              index: "checked",
              val1: false,
              childName: "productSkuList"
      })
      } else if (!item.productSkuList) {
        /* 当是子集的时候，选择一个对应的父级就选上 */
        let status = false;
        if (!todo) {
          return false;
        }
        for (let child of todo.productSkuList) {
          if (child.checked) {
            status = true;
          }
        }
        if (!status) {
          //子集没有被选中
          todo.checked = false;
        } else {
          todo.checked = true;
        }
      }
    },
    onJoinSale(item, todo) {
      //点击加入促销的时候触发的事件
      item.checked = !item.checked;
      this.onJoinSaleBody(item, todo);
      this.$refs.editTable.selectAll = getSelectStatus(
        this.$store.state.fullGiftSale.GetgiftData3,
        { index: "checked", childName: "productSkuList" }
      );
    },
    onJoinSaleCheck(item, todo) {
      // 点击checked按钮的时候触发的事件
      this.onJoinSaleBody(item, todo);
      this.$refs.editTable.selectAll = getSelectStatus(
        this.$store.state.fullGiftSale.GetgiftData3,
        { index: "checked", childName: "productSkuList" }
      );
    },
    onDeleteSale(index, data, index1, data1) {
      deleteSale(index, data, index1, data1);
    }
  }
};
</script>

